<template>
  <van-icon
    color="#777"
    :name="attitude === 1 ? 'good-job' : 'good-job-o'"
    @click="onLike"
  />
</template>

<script>
import { likeArtical, dislikeArtical } from '@/api/artical.js';
export default {
  name: 'LiketArtical',
  props: {
    attitude: {
      type: Number,
      required: true,
    },
    userId: {
      type: [Number, String, Object],
      required: true,
    },
  },
  data() {
    return {};
  },
  methods: {
    async onLike() {
      let status = -1;
      try {
        if (this.attitude === 1) {
          //   已经点过赞了，取消点赞
          await dislikeArtical(this.userId);
          this.$toast('已取消点赞！');
        } else {
          await likeArtical(this.userId);
          status = 1;
          this.$toast('已经点赞！');
        }
      } catch (err) {
        console.log(err);
        this.$toast('操作失败，请稍后重试！');
      }
      this.$emit('updateLike', status);
    },
  },
};
</script>

<style lang="less" scoped></style>
